<script setup lang="ts">
import { computed, reactive, ref } from 'vue'
// 组件
import { ElMessage } from 'element-plus'
// 接口
import type { IUserFormData, IUserData } from '@/api/system/user/type'
import type { ResponseData } from '@/types/common'
import type { FormRules, FormInstance } from 'element-plus'
// api
import { reqUserAdd, reqUserEdit } from '@/api/system/user/index'
// 组件参数
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
  },
})
// 组件事件
const emit = defineEmits(['update:modelValue', 'refresh-list'])
// 计算属性
const visible = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emit('update:modelValue', val)
  },
})
// 表单参数
let form = reactive<IUserFormData>({
  username: '',
  name: '',
  password: '',
})
// 表单
const formRef = ref<FormInstance>()
// 表单校验
const rules = reactive<FormRules<IUserFormData>>({
  username: [{ required: true, message: '请输入用户名', trigger: 'change' }],
  name: [{ required: true, message: '请输入姓名', trigger: 'change' }],
  password: [{ required: true, message: '请输入密码', trigger: 'change' }],
})

// 弹窗关闭
const close = () => {
  // 重置表单
  formRef.value?.resetFields()
}
// 点击取消
const handleCancel = () => {
  visible.value = false
}
// 点击确认
const handleSubmit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid) => {
    if (valid) {
      const { code, message }: ResponseData = await (form.id
        ? reqUserEdit(form)
        : reqUserAdd(form))
      if (code === 200) {
        ElMessage({
          message: message,
          type: 'success',
        })
        visible.value = false
        emit('refresh-list')
      } else {
        ElMessage({
          message: message,
          type: 'error',
        })
        emit('refresh-list')
      }
    }
  })
}

// 表单初始化
const setFormData = (row: IUserData) => {
  const { id, username, name, password } = row
  form = reactive<IUserFormData>({
    id,
    username,
    name,
    password,
  })
}
// 子组件暴露方法
defineExpose({ setFormData })
</script>
<template>
  <el-dialog
    v-model="visible"
    :title="form.id ? '编辑' : '新增'"
    width="500"
    @close="close"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="70">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="form.username"
          placeholder="请输入用户名"
          clearable
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleSubmit(formRef)">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
